package com.itheima.ssm.dao;

import com.itheima.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;

import java.util.List;

public interface IUserDao {

    @Select("select * from users where username=#{name}")
    @Results({
            @Result(property = "id",id = true, column = "id"),
            @Result(property = "roles", column = "id", javaType = List.class,many = @Many(select = "com.itheima.ssm.dao.IRoleDao.findById"))
    })
    public UserInfo findByName(String name) throws Exception;


/*==================================================================================================*/

@Select("select * from users")
@Results({
        @Result(id = true ,property = "id",column = "id"),
        @Result(property = "roles", column = "id" ,javaType = List.class,
        many =@Many(select = "com.itheima.ssm.dao.IRoleDao.findById"))
})
    public List<UserInfo> findAll();
/*================================================================================================*/
@SelectKey(statement = "select SYS_GUID() as id from dual",
        keyProperty = "id",before = true,keyColumn = "id",resultType = String.class)

@Insert("insert into users(id,username,password,email,phoneNum,status) values(#{id},#{username},#{password}," +
        "#{email},#{phoneNum},#{status})")
    public void save(UserInfo users);
/*===================================================================*/

@Select("select *from users where id=#{id}")
@Results({
        @Result(id = true,property ="id",column = "id"),
        @Result(property = "roles", column = "id", javaType = List.class,many = @Many(select = "com.itheima.ssm.dao.IRoleDao.findById"))
})
    UserInfo findById(String id);
/*===========================================================================*/

@Insert("insert into users_role (userId,roleId) values(#{id},#{roleId})")
    void insertUserAndRole(@Param("id") String id, @Param("roleId") String roleId);
}
